export function projectFunc() {

    const getToken= async(code:string)=>{

        const clientId =  import.meta.env.VITE_APP_clientId; // 示例中的 client_id
        const clientSecret =  import.meta.env.VITE_APP_clientSecret; // 示例中的 client_secret
        const redirectUri = import.meta.env.VITE_APP_redirectUrl
        const ipPort = import.meta.env.VITE_APP_ipPort;
  
        const tokenUrl = `${ipPort}agcloud-sso/oauth/token`;
        const credentials = btoa(`${clientId}:${clientSecret}`);
        const headers = {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Authorization': `Basic ${credentials}`
        };
  
        const body = `grant_type=authorization_code&code=${code}&redirect_uri=${redirectUri}`;
        try{
          const response = await fetch(tokenUrl, {
            method: 'POST',
            headers: headers,
            body: body
          });
    
          return response.json(); // 包含 access_token
        }catch (error) {
      console.error('Request Error:', error);
    } 
    }
    const goback=()=>{
        let authUrl = 'http://www.hycim.com/agcim-hy-index/'
        window.open(authUrl, "_self");
    }
  
      const randomProjectName=()=>{
        const projectList = [
          "晴川项目",
          "龙阳项目",
          "建桥项目",
          "鹦鹉项目",
          "洲头项目",
          "四新项目",
          "五里墩项目",
          "琴断口项目",
          "江汉二桥项目",
          "永丰项目",
          "江堤项目",
        
        ];
        return projectList[Math.floor(Math.random() * projectList.length)];
      }
      const getRandomDateBetweenYears=()=> {
        let startYear = 2021;
        let endYear = 2023;
        const start = new Date(startYear, 0, 1);
        const end = new Date(endYear, 11, 31, 23, 59, 59);
        const randomDate = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
    
        const formatNumber = (num:any) => (num < 10 ? '0' + num : num);
        const year = randomDate.getFullYear();
        const month = formatNumber(randomDate.getMonth() + 1); // 月份是从0开始的
        const day = formatNumber(randomDate.getDate());
        const hours = formatNumber(randomDate.getHours());
        const minutes = formatNumber(randomDate.getMinutes());
        const seconds = formatNumber(randomDate.getSeconds());
    
        return `${year}-${month}-${day} ${hours}-${minutes}-${seconds}`;
    }
    const randomArea=()=>{
      const projectList = [
        "晴川街道",
        "龙阳街道",
        "建桥街道",
        "鹦鹉街道",
        "洲头街道",
        "四新街道",
        "五里墩街道",
        "琴断口街道",
        "江汉二桥街道",
        "永丰街道",
        "江堤街道",
        "汉阳经济开发区",
        "四新地区管委会",
      
      ];
      return projectList[Math.floor(Math.random() * projectList.length)];
    }
    const randomNum=()=>{
      const randomNumber = Math.floor(Math.random() * 1000000);
      // 将生成的随机数转换为字符串，并用前导零填充到6位
      return randomNumber.toString().padStart(6, '0');
  
    }
    const hexToRGBA=(hexColor:any, alpha = 1.0)=>{
      // 移除颜色代码前的 '#' 符号
      hexColor = hexColor.replace('#', '');
      // 将十六进制颜色代码转换为整数的 RGB 值
      const r = parseInt(hexColor.substring(0, 2), 16);
      const g = parseInt(hexColor.substring(2, 4), 16);
      const b = parseInt(hexColor.substring(4, 6), 16);
      // 返回带有透明度的 RGBA 颜色字符串
      return `rgba(${r}, ${g}, ${b}, ${alpha})`;
  }
return{
    getToken,
    goback,
    randomProjectName,
    getRandomDateBetweenYears,
    randomArea,
    randomNum,
    hexToRGBA
}
}